package dao.impl;

import bean.MerType;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import dao.MerTypeDao;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import utils.c3p0Utils;

import java.sql.SQLException;
import java.util.List;

/**
 * @program:Supermarket)
 * @类名：
 * @author:
 * @create:2020-08-20)
 **/
public class MerTypeImpl implements MerTypeDao {


    @Override
    public int MerTypeAdd(String typename) {
        String sql="INSERT INTO MerType (typename,isalive) VALUES (?,1)";
        QueryRunner qr=new QueryRunner(c3p0Utils.getDataSource());
        int i=0;
        try {
            i=qr.update(sql,typename);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }

    @Override //删除
    public int MerTypeDel(int mertypeId) {
        QueryRunner qr=new QueryRunner(c3p0Utils.getDataSource());
        int i=0;
        try {
            i=qr.update("update MerType set isalive=? where mertypeId = ?",0,mertypeId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;

    }
    @Override
    public int MerTypeChange(int mertypeId, String typename) {
        QueryRunner qr=new QueryRunner(c3p0Utils.getDataSource());
        int i=0;
        try {
            i=qr.update("update MerType set typename=? where mertypeId=?",typename,mertypeId);//
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }

    @Override
    public PageInfo whList(int page, int size) {
        QueryRunner qr=new QueryRunner(c3p0Utils.getDataSource());
        PageHelper.startPage(page,size);
        List<MerType> list=null;
        try {
            list=qr.query("select * from MerType where isalive=?",new BeanListHandler<MerType>(MerType.class),1);//
            System.out.println(list);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        PageInfo<MerType> pageinfo=new PageInfo<>(list);

        return pageinfo;
    }

    @Override
    public MerType MerTypeSingal(int mertypeId) {
        QueryRunner qr=new QueryRunner(c3p0Utils.getDataSource());
        MerType wh=null;
        try {
            wh=qr.query("select * from MerType where mertypeId=? and isalive=?",new BeanHandler<MerType>(MerType.class),mertypeId,1);//

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return wh;
    }

    @Override
    public PageInfo MerTypeOne(String typename) {
        QueryRunner qr = new QueryRunner(c3p0Utils.getDataSource());
        List<MerType> whOne = null;
        try {
            whOne = qr.query("select * from MerType where typename=? and isalive=?", new BeanListHandler<MerType>(MerType.class), typename, 1);//

        } catch (SQLException e) {
            e.printStackTrace();
        }
        PageInfo<MerType> pageinfo = new PageInfo<>(whOne);
        return pageinfo;
    }



}
